package com.csc.movie.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csc.movie.common.util.DateUtil;
import com.csc.movie.model.entity.Movie;
import com.csc.movie.model.base.result.ResponseVo;
import com.csc.movie.model.vo.MovieVO;
import com.csc.movie.service.MovieService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/movie")
public class MovieController {
    @Autowired
    MovieService movieService;

    //从虎嗅资源网拿数据https://huyazy.com/
    @GetMapping("/getMovieListFromHuYa/{page}")
    public ResponseVo getMovieListFromHuYa(@PathVariable("page")Integer page) {
//        String result = HttpClientUtil.doGet("https://huyazy.com/");
        // System.out.println(result);
        log.info("nihao");
//        log.error("result is "+result);

        // 通过Jsoup连接到指定的网页并获取其内容
        try {
            if(page==null){
                page=1;
            }
            String url="https://huyazy.com/index.php/vod/type/id/23/page/"+page+".html?ac=detail";
            Document doc = Jsoup.connect(url).get();
            Elements xing_vb = doc.getElementsByClass("xing_vb");
            Element element = xing_vb.get(0);
            Elements liboders = element.getElementsByClass("liboder");
            List<Movie> movieList = new ArrayList<>();
            for (int i = 0; i < liboders.size(); i++) {
                Elements xing_vb4 = liboders.get(i).getElementsByClass("xing_vb4");
                String movieName = xing_vb4.get(0).text();
                log.info(movieName);
                String movieDetailUrl = xing_vb4.get(0).getElementsByTag("a").get(0).attr("href");
                log.info(movieDetailUrl);
                String type = xing_vb4.get(0).getElementsByClass("emtext").text();
                log.info(type);
                String classify = liboders.get(i).getElementsByClass("xing_vb5").get(0).text();
                log.info(classify);
                String updateTime = liboders.get(i).getElementsByClass("xing_vb7").get(0).text();
                log.info(updateTime);
                String realMovieName = movieName.replace(type, "");
                log.info(realMovieName);
                Movie movie = new Movie();
                movie.setName(realMovieName);
                movie.setType(type);
                movie.setUpdateTime(DateUtil.dateFormat(updateTime));
                movieList.add(movie);
//                movieService.save(movie);
            }
            List<Movie> list = movieService.list();
            movieList.removeAll(list);
            if(movieList!=null&&movieList.size()>0){
                movieService.saveBatch(movieList);
            }
        } catch (Exception e) {

            e.printStackTrace();
            return ResponseVo.error();
        }

        return ResponseVo.ok();
    }

    @ApiOperation("获取电影列表 分页1")
    @GetMapping("list/{pageNum}/{pageSize}")
    public ResponseVo list(MovieVO movieVO,
                           @PathVariable("pageNum")Integer pageNum,
                           @PathVariable("pageSize")Integer pageSize){//pojo入参
        Page<MovieVO> page = movieService.getMovicesByCondition(movieVO,pageNum,pageSize);
        return ResponseVo.ok().data("page",page);
    }

    @ApiOperation("添加电影信息")
    @PostMapping("saveMovie")
    public ResponseVo save(@RequestBody MovieVO movieVO){
       movieService.saveMovie(movieVO);
        return ResponseVo.ok().message("保存成功");
    }
}
